package com.mycompany.myapp.service.impl;

import com.mycompany.myapp.service.CollectionService;
import com.mycompany.myapp.domain.Collection;
import com.mycompany.myapp.repository.CollectionRepository;
import com.mycompany.myapp.service.dto.CollectionDTO;
import com.mycompany.myapp.service.mapper.CollectionMapper;
import com.mycompany.myapp.web.rest.utils.ResultObj;
import com.mycompany.myapp.web.rest.utils.TypeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Type;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing {@link Collection}.
 */
@Service
@Transactional
public class CollectionServiceImpl implements CollectionService {

    private final Logger log = LoggerFactory.getLogger(CollectionServiceImpl.class);

    private final CollectionRepository collectionRepository;

    private final CollectionMapper collectionMapper;

    public CollectionServiceImpl(CollectionRepository collectionRepository, CollectionMapper collectionMapper) {
        this.collectionRepository = collectionRepository;
        this.collectionMapper = collectionMapper;
    }

    /**
     * Save a collection.
     *
     * @param collectionDTO the entity to save.
     * @return the persisted entity.
     */
    @Override
    public CollectionDTO save(CollectionDTO collectionDTO) {
        log.debug("Request to save Collection : {}", collectionDTO);
        Collection collection = collectionMapper.toEntity(collectionDTO);
        collection = collectionRepository.save(collection);
        return collectionMapper.toDto(collection);
    }

    /**
     * Get all the collections.
     *
     * @return the list of entities.
     */
    @Override
    @Transactional(readOnly = true)
    public List<CollectionDTO> findAll() {
        log.debug("Request to get all Collections");
        return collectionRepository.findAll().stream()
            .map(collectionMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }


    /**
     * Get one collection by id.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    @Override
    @Transactional(readOnly = true)
    public Optional<CollectionDTO> findOne(Long id) {
        log.debug("Request to get Collection : {}", id);
        return collectionRepository.findById(id)
            .map(collectionMapper::toDto);
    }

    /**
     * Delete the collection by id.
     *
     * @param id the id of the entity.
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete Collection : {}", id);
        collectionRepository.deleteById(id);
    }

    public ResultObj createCollection(CollectionDTO collectionDTO){
        List<Collection> collections =findByUserId(collectionDTO.getSuserId());
        if(!TypeUtils.isEmpty(collectionDTO)){
            for(Collection c : collections){
                if(c.getCommodity().getId() == collectionDTO.getCommodityId()){
                    return ResultObj.backInfo(true,200,"已有此收藏！",null);
                }
            }
            save(collectionDTO);
            return ResultObj.backInfo(true,200,"收藏成功！",null);
        }
        return ResultObj.backInfo(true,200,"收藏失败！",null);
    }

    public List<Collection> findByUserId(Long userId){
        return collectionRepository.findAllBySuserId(userId);
    }

    public ResultObj deleteCollections(List<Long> collections){
        if(!TypeUtils.isEmpty(collections)){
            for(Long id : collections){
                delete(id);
            }
        }
        return ResultObj.backInfo(true,200,"删除成功",null);
    }


}
